import NProgress from 'nprogress';

export function query(url, method = 'GET', data, headers) {
    NProgress.start();
    // 显示进度条
    const baseURL = `http://vue-server.duans.top:10041/apis/`;
    const options = {
        method,
        headers: Object.assign({
            'Content-Type': 'application/x-www-form-urlencoded',
            'Token': sessionStorage.getItem('token')
        }, headers)
    }
    if (data) {
        const arr = [];
        for (let key in data) {
            arr.push(`${key}=${data[key]}`);
        }
        options.body = arr.join('&');
    }
    return fetch(`${baseURL}${url}`, options).then(response => {
        // 隐藏进度条
        NProgress.done();
        return response.json();
    });
}

export function dateFmt(input, divider = '-') {
    const obj = new Date(input);
    const y = obj.getFullYear();
    const m = obj.getMonth() + 1;
    const d = obj.getDate();
    const h = obj.getHours();
    const mm = obj.getMinutes();
    const s = obj.getSeconds();
    return `${y}${divider}${m}${divider}${d} ${h}:${mm}:${s}`;
}